package com.bdth.weget.home.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.bdth.weget.common.MessageResult;
import com.bdth.weget.common.Pagination;
import com.bdth.weget.home.entity.Cars;
import com.bdth.weget.home.entity.User;
import com.bdth.weget.home.service.CarsService;
import com.bdth.weget.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/7/9.
 */
@Controller
@RequestMapping("/cars")
public class CarsController extends BaseController {

    @Autowired
    private CarsService carsService;

    @RequestMapping(value = "index")
    public String index() {
        return "cars/index";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<Cars> getPageList(HttpServletRequest request,Pagination<Cars> pagination, @RequestParam(required = false) String simno,
                                    @RequestParam(required = false) String carno,
                                    @RequestParam(required = false) String plateno){
        Map<String, Object> query = new HashMap<>();
        query.put("simno",simno);
        query.put("carno",carno);
        query.put("plateno",plateno);
        User session = (User)request.getSession().getAttribute("loginUser");
        if(session.getGroupid()!=0) {
            query.put("departids", session.getDepartids());
        }
        Page<Cars> page = carsService.listPages(pagination,query);
        PageVo<Cars> pageVo = new PageVo<>(page.getRecords(),page.getTotal());
        return pageVo;
    }

    @RequestMapping(value = "all")
    @ResponseBody
    public List<Cars> getAllList(){
        return carsService.listAll();
    }

    @RequestMapping(value = "form")
    public String form(){
        return "cars/form";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public MessageResult save(Cars param){
        boolean flag = carsService.save(param);
        if(flag)
            return MessageResult.getSuccess("添加数据成功");
        else
            return MessageResult.getError("500","添加数据失败");
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public MessageResult update(Cars param){
        boolean flag = carsService.update(param);
        if(flag)
            return MessageResult.getSuccess("修改数据成功");
        else
            return MessageResult.getError("500","修改数据失败");
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public MessageResult delete(@RequestParam Integer ids){
        boolean flag = carsService.delete(ids);
        if(flag)
            return MessageResult.getSuccess("删除数据成功");
        else
            return MessageResult.getError("500","删除数据失败");
    }

    @RequestMapping(value = "remove")
    @ResponseBody
    public MessageResult deletes(@RequestParam String ids){
        boolean flag = carsService.remove(ids);
        if(flag)
            return MessageResult.getSuccess("删除数据成功");
        else
            return MessageResult.getError("500","删除数据失败");
    }

    @RequestMapping(value = "search")
    public String search(){
        return "cars/search";
    }
}